# 1、连接方式
import redis

r = redis.Redis(host='192.168.127.130', port=6379, db=0, password="123456")
# r.set('name', 'asdasd')  # 添加
print(r.get('name'))  # 获取
print(r.lrange('a_list', 0, 3))  # 获取
print(r.lrange('b_list', 0, 3))  # 获取
# r.lpush('c_list', 0)
# r.lpush('c_list', 1)
# r.lpush('c_list', 2)
# r.lpush('c_list', 3)
# r.lpop('c_list')
c = r.lrange('c_list', 0, 3)
print(c)
print(id(c))
print(c[0])
print(id(c[0]))
print(id(c[1]))
print(id(c[2]))
# 2、连接池
# import redis
#
# pool = redis.ConnectionPool(host='192.168.127.130', port=6379, password="123456")
# r = redis.Redis(connection_pool=pool)
# r.set('name1', 'abc')  # 添加
# r.set('name1', 'efg')  # 添加
# r.set('name2', '123')  # 添加
# print(r.get('name'))  # 获取
# print(r.get('name1'))  # 获取
# print(r.get('name2'))  # 获取

# 4、管道
# import redis
#
# pool = redis.ConnectionPool(host='192.168.127.130', port=6379, password="123456")
# r = redis.Redis(connection_pool=pool)
#
# pipe = r.pipeline(transaction=True)
#
# pipe.set('name', 'ttt')
# pipe.set('name', 'ggg')
# pipe.lpush('a_list', 11)
# pipe.lpush('a_list', 12)
# pipe.lpush('a_list', 13)
# pipe.lpush('a_list', 14)
# r.lpush('b_list', 11)
# r.lpush('b_list', 12)
# r.lpush('b_list', 13)
# r.lpush('b_list', 14)
# print(r.get('name'))
# # print(pipe.get('name'))
# pipe.execute()
# print(r.get('name'))

# 5、发布和订阅
# import redis
#
#
# class RedisHelper(object):
#     def __init__(self):
#         self.__conn = redis.Redis(host='192.168.127.130', port=6379, password="123456")  # 连接Redis
#         self.channel = 'monitor'  # 定义名称
#
#     def publish(self, msg):  # 定义发布方法
#         self.__conn.publish(self.channel, msg)
#         return True
#
#     def subscribe(self):  # 定义订阅方法
#         pub = self.__conn.pubsub()
#         pub.subscribe(self.channel)
#         pub.parse_response()
#         return pub
#
